package moodish.client.gui.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;

import moodish.client.ClientSideCommManeger;
import moodish.client.gui.MoodishClientGui;
import moodish.comm.ClientComm;

public class Sentinel implements ActionListener {

	private MoodishClientGui gui;
	private ClientSideCommManeger commManager;
	private ClientComm comm;

	public Sentinel(MoodishClientGui gui) {
		this.gui = gui;
		this.commManager = new ClientSideCommManeger();
		this.comm = gui.getComm();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();

		if (button == gui.getSimpleLogin().getLogin()) {
			try {
				commManager.connectToServer(comm, gui);
			} catch (IOException e1) {
				e1.printStackTrace();
			}

		} else if (button == gui.getMainPanel().getSendButton()) {
			// TODO send; send new mood to friends
			commManager.sendMoodishMessage(comm, gui);

		} else if (button == gui.getMainPanel().getDisconnect()) {
			// TODO send; send message "DISCONECT" to server
			commManager.disconnectClient(comm, gui);

		} else if (button == gui.getMainPanel().getSendFriendShipButton()) {
			// TODO send; send friendship request
			commManager.sendFriendShipRequest(comm, gui);

		} else if (button == gui.getMainPanel().getSendUnFriendShipButton()) {
			// TODO send; send unfriendship request
			commManager.sendUnfriendShipRequest(comm, gui);

		} else if (button == gui.getMainPanel().getSearchButton()) {
			// TODO search; search friends by mood
			commManager.searchFriends(gui.getMainPanel().getSearchField().getText(), gui);
		}
	}

	public ClientComm getComm() {
		return comm;
	}

}
